package com.example.mecalculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    private TextView meTv;
    private TextView meTitle;

    // 整数
    private static int numI = 0;

    // 小数
    private static double numD = 0;

    // 同步标题
    private static String title = "";

    // 显示字符
    private String meText = "";

    private String meText1 = "";

    // 当前的运算符
    private String meText2 = "";

    //判断是否有小数参与运算
    private boolean flag = false;

    // 记录有几位小数
    private int s = 0;

    //是否继续运算
    private boolean flag2 = true;

    private boolean flag3 = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        meTitle = findViewById(R.id.me_title);
        meTv = findViewById(R.id.me_tv);
    }

    //数字输入
    public void meClick(View view) {

        Button button = (Button)view;
        if (!flag2 && meText2.equals("") && !flag) {
            clearAllData();
        }else {
            flag2 = false;
        }

        returnNumber(button);

    }

    // 清除数据
    private void clearAllData() {
        title = "";
        meText = "";
        meText1 = "";
        setMetv("");
        setMetitle();
        flag3 = false;
        meText2 = "";
    }

    // 查看结果
    public void returnNumber(Button button) {
        // 获取当前按下的数字
        String str = (String) button.getText();

        // 判断是否已经选择了运算符
        if (meText2.equals("")) {
            if (flag) {
                if (s > 0) {
                    numD += numD + numI;
                }
                double d = Double.parseDouble(str);
                s++;
                for (int i = 0; i < s; i++) {
                    d *= 0.1;
                }
                numD = numI + d;
                meText = numD + "";
            }else {
                // 传入同步标题数据
                meText += str;

                numI = Integer.parseInt(meText);
            }
            setMetv(meText);
        }else {
            if (flag) {
                if (s > 0) {
                    numD += numD + numI;
                }
                double d = Double.parseDouble(str);
                s++;
                for (int i = 0; i < s; i++) {
                    d *= 0.1;
                }
                numD = numI + d;
                meText1 = numD + "";
            }else {
                meText1 += str;
                numI = Integer.parseInt(meText1);
            }
            setMetv(meText1);
        }

    }

    // 都是整数时的运算
    public int getN(int a,int b,String c) {
        switch (c) {
            case "+":
                a += b;
                break;
            case "-":
                a -= b;
                break;
            case "×":
                a *= b;
                break;
            case "÷":
                a /= b;
                break;
        }
        return a;
    }

    // 有小数参数的运算
    public Double getN(double a,double b,String c) {
        switch (c) {
            case "+":
                a += b;
                break;
            case "-":
                a -= b;
                break;
            case "×":
                a *= b;
                break;
            case "÷":
                a /= b;
                break;
        }
        return a;
    }


    // 运算方法
    public void meStrClick(View view) {

        Button button = (Button) view;
        String str = (String) button.getText();
        meText2 = str;
        title += meText + " " + str + " ";
        if (meText1.equals("")) {
            title = meText + " " + str + " ";
            flag2 = true;
        }

        flag = false;
        setMetitle();

    }

    // 显示结果
    public void retuan(View view) {

        // 完整填充同步标题
        title += meText1 + " " + "=";
        setMetitle();

        // 判断是否有小数参与运算
        if (flag3) {
            meText = getN(Double.parseDouble(meText),Double.parseDouble(meText1),meText2) + "";
        }else {
            meText = getN(Integer.parseInt(meText),Integer.parseInt(meText1),meText2) + "";
        }

        flag = false;
        // 清空运算符
        meText2 = "";
        title = meText + " ";
        meText1 = "";
        setMetv(meText);
    }

    // 输入头部标题数据
    public void setMetitle() {
        // 将数据传入头部标题
        meTitle.setText(title);
    }

    // 输入显示的数据
    public void setMetv(String str) {
        meTv.setText(str);
    }

    // 生成小数
    public void doubleDian(View view) {
        if (!flag) {
            meTv.setText(meTv.getText() + ".");
        }
        flag3 = true;
        s = 0;
        numD = 0;
        flag = true;
    }

    public void clearClick(View view) {
        if (meText2.equals("")) {
            if (flag) {

                for (int i = 0; i < s; i++) {
                    numD *= 10;
                }
                numD %= 10;
                s--;
                for (int i = 0; i < s; i++) {
                    numD *= 0.1;
                }
                if (s == 0) {
                    flag = false;
                    flag3 = false;
                }
                meText = numD + "";
                setMetv(meText);
            }else {
                numI /= 10;
                meText = numI + "";
                setMetv(meText);
                if (numI == 0) {
                    meText = "";
                }
            }
        }else {
            if (flag) {
                for (int i = 0; i < s; i++) {
                    numD *= 10;
                }
                numD %= 10;
                s--;
                for (int i = 0; i < s; i++) {
                    numD *= 0.1;
                }
                if (s == 0) {
                    flag = true;
                }

                meText1 = numD + "";
                setMetv(meText1);
            }else {
                numI %= 10;
                meText1 = numI + "";
                setMetv(meText1);
            }
        }
    }

    public void clearData(View view) {
        clearAllData();
        numI = 0;
        numD = 0;
        setMetv("0");
    }
}